package com.copractice.bl.user;

import com.copractice.data.dao.knowledge.UsersKnowledgeDAO;
import com.copractice.data.dao.tag.TagDAO;
import com.copractice.data.dao.user.UserDAO;
import com.copractice.data.entity.knowledge.UsersKnowledge;
import com.google.inject.Inject;

/**
 * Contains business logic methods of working with users knowledge entities.
 * 
 * @author Dmitry Churbanov (dmitry.churbanov@gmail.com)
 * @version 1.0 04/22/2009
 */
public class UserKnowledgeBL {
    
    UsersKnowledgeDAO usersKnowledgeDAO;
    UserDAO usersDAO;
    TagDAO tagsDAO;
    
    /**
     * Creates and saves <code>UsersKnowledge</code> object for the user
     * with <code>userId</code> identifier and tag with <code>tagId</code>
     * identifier if current record does not exists
     */
    public void createUsersKnowledgeIfNecessary(Long userId, Long tagId) {
        UsersKnowledge usKn = usersKnowledgeDAO.fetchUserKnowledgeByUserIdAndTagId(userId, tagId);
        
        if (usKn == null) {
            usKn = new UsersKnowledge();
            usKn.setNumberQuestions(0L);
            usKn.setTag(tagsDAO.getById(tagId));
            usKn.setTeachingScore(0L);
            usKn.setTotalPossibleScore(0L);
            usKn.setTotalScore(0L);
            usKn.setUser(usersDAO.getById(userId));
            
            usersKnowledgeDAO.save(usKn);
        }
    }

    public UsersKnowledgeDAO getUsersKnowledgeDAO() {
        return usersKnowledgeDAO;
    }

    @Inject
    public void setUsersKnowledgeDAO(UsersKnowledgeDAO usersKnowledgeDAO) {
        this.usersKnowledgeDAO = usersKnowledgeDAO;
    }

    public UserDAO getUsersDAO() {
        return usersDAO;
    }

    public void setUsersDAO(UserDAO usersDAO) {
        this.usersDAO = usersDAO;
    }

    public TagDAO getTagsDAO() {
        return tagsDAO;
    }

    public void setTagsDAO(TagDAO tagsDAO) {
        this.tagsDAO = tagsDAO;
    }    

}
